﻿using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
//using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;
using GHI =  Gadgeteer.Modules.GHIElectronics;
using GHIElectronics.Gadgeteer;

namespace Gadgeteer.Modules.Community.XBee.Samples.Hydra
{
    public partial class Program
    {
        XBeeClient client1;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            Debug.Print("Program Started");
            
            // NOTE: you must connect the XBee module to socket 4, OR update the following line with the socket you 
            // have connected the XBee module to. The GHI XBee module can be connected to socket types 'K' and 'U'.
            // On The Spider, the valid sockets are 4, 8, and 11
            client1 = new XBeeClient(4);

            // you must register event handlers for any events you are interested in

            client1.TransmitStatusReceived += new TransmitStatusReceivedEventHandler(client1_TransmitStatusReceived);
            client1.TransmitReceive+=new TransmitReceiveEventHandler(client1_TransmitReceive);
            client1.DataReceived += new DataReceivedEventHandler(client1_DataReceived);
            client1.StringReceived += new StringReceivedEventHandler(client1_StringReceived);
            client1.ATResponseReceived+=new ATResponseReceivedEventHandler(client1_ATResponseReceived);

            client1.SendLocalATCommandAsync("MY");
            client1.SendLocalATCommandAsync("SH");
            client1.SendLocalATCommandAsync("SL");

            client1.UploadStringAsync("hello from Hydra!");
        }

        private void WriteStringToDisplay(string str)
        {
            uint width = display_T35.Width;
            uint height = display_T35.Height;
            Font font = Resources.GetFont(Resources.FontResources.small);
            display_T35.SimpleGraphics.DisplayEllipse(GT.Color.Blue, width / 4, height / 4, width / 4, height / 4);
            display_T35.SimpleGraphics.DisplayRectangle(GT.Color.Red, 2, GT.Color.Gray, width / 4, height / 4, width / 2, height / 2, 10, 0xFFFF);
            display_T35.SimpleGraphics.DisplayTextInRectangle(str, width / 4 + 5, height / 4 + 5, width / 2 - 10, height / 2 - 10, GT.Color.Green, font,
                Gadgeteer.Modules.GHIElectronics.Display_T35.DisplayModule.SimpleGraphicsInterface.TextAlign.Center);
        }

        void client1_StringReceived(object sender, StringReceivedEventArgs e)
        {
            Debug.Print("String Received: " + e.Message);

            WriteStringToDisplay(e.Message);
        }

        void client1_DataReceived(object sender, DataReceivedEventArgs e)
        {
            Debug.Print("Data Received: " + e.Data.Length);
        }

        void client1_TransmitStatusReceived(object sender, TransmitStatusReceivedEventArgs e)
        {
            Debug.Print("Transmit Success: " + e.Result.DeliveryStatus);
        }

        void client1_TransmitReceive(object sender, TransmitReceiveEventArgs e)
        {
            Debug.Print(e.Result.ToString());
        }

        void client1_ATResponseReceived(object sender, ATResponseReceivedEventArgs e)
        {
            Debug.Print(e.Result.ToString());
        }

        void client1_DiscoverNodesCompleted(object sender, DiscoverNodesCompletedEventArgs e)
        {
            Debug.Print("Discover nodes success");
        }
       
    }
}
